<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\TemplatesModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\templates;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\TemplatesForm;

class TemplatesController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $templates_model = new TemplatesModel($em);
        $templates = $templates_model->findAll();
        return $this->render('BillingBundle:Templates:index.html.twig', array('templates' => $templates));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $templates_model = new TemplatesModel($em);
        
        $template = new templates();
        //$templates_model->create($template->initDefaultEntity());//создание по умолчанию
        //return new RedirectResponse($this->generateUrl('templates_show'));
        
        $form = $this->createForm(new TemplatesForm(), $template);
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $templates_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('18');//страница шаблоны
                $log->setObjectId($template->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание шаблона
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('templates_show'));
            }
        }
        return $this->render('BillingBundle:Templates:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $templates_model = new TemplatesModel($em);
        $template = $templates_model->findByPK($id);

        $form = $this->createForm(new TemplatesForm(), $template);
        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $templates_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('18');//страница шаблоны
                $log->setObjectId($template->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение шаблона
                $log_model->create($log);

                return new RedirectResponse($this->generateUrl('templates_show'));
            }
        }
        return $this->render('BillingBundle:Templates:update.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $templates_model = new TemplatesModel($em);
        
        $template = $templates_model->findByPK($id);
        $securityContext = $this->container->get('security.context');
        
        $templates_model->removeByPK($id);
        $userCurrent = $securityContext->getToken()->getUser();
        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('18');//страница шаблоны
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление шаблона
        $log_model->create($log);
            
        return new RedirectResponse($this->generateUrl('templates_show'));
    }
}